package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.base.*;
import com.yxh.common.annotation.Authorization;
import com.yxh.common.other.Const;
import com.yxh.dao.UserMapper;
import com.yxh.pojo.FlowShip;
import com.yxh.pojo.User;
import com.yxh.pojo.tree.BaseTreeGrid;
import com.yxh.pojo.vo.UserRestPasswordVO;
import com.yxh.service.UserService;
import com.yxh.util.Response;
import com.yxh.util.TreeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yxh.util.APIErrorCode.SUCCESS;

/**
 * Created by 32988 on 2018/5/31.
 */
@Scope("prototype")
@RestController
@RequestMapping("/user/")
public class UserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    private Response response;

    @Autowired
    private UserService userService;

    /**
     * @param user
     * @return
     */
    @PostMapping("register")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response register(@RequestBody User user) {
        userService.register(user);
        return Response.returnResponse(SUCCESS);
    }

    /**
     * @param userRestPasswordVO
     * @return
     */
    @PutMapping("reset_password")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response resetPassword(@RequestBody UserRestPasswordVO userRestPasswordVO) {
        userService.resetPassword(userRestPasswordVO);
        return Response.returnResponse(SUCCESS);
    }

    /**
     * @param user
     * @return
     */
    @GetMapping("get_information")
   // @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getInformation(User user, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = userService.getInformation(user, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }

    /**
     * @param
     * @return
     */
    @GetMapping("get_userInfo")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getUserInfoByMoreData(String userInfo, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = userService.getUserInfo(userInfo, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }


    /**
     * @param user
     * @return
     */
    @RequestMapping(value = "update_userInfo", method = RequestMethod.PUT)
   // @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response updateUserInfo(@RequestBody User user) {
        userService.update(user);
        response = Response.returnResponse(SUCCESS);
        return response;
    }

    /**
     * @return
     */
    @DeleteMapping("/{id}")
   @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response deleteUser(@PathVariable("id") String id) {
        userService.delete(id);
        response = Response.returnResponse(SUCCESS);
        return response;
    }
    /** 根据部门id 查所在部门下所有人员
     * @param deptId
     * @return
     */
    @GetMapping("/{deptId}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getInformation(@PathVariable("deptId") String deptId) {

        List<User> pageInfo = userService.getInformation(deptId);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }

    /**
     * 部门 人员合成树的形式 返回
     * @return
     */
    @GetMapping("list")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getList(FlowShip flowShip) {

        //List<User> pageInfo = userService.getInformation(deptId);

          response = Response.returnResponse(SUCCESS);
          response.setData(userService.getList(flowShip));

        return response;
    }


    /**
     * 部门 人员合成树的形式 返回
     * @return
     */
    @GetMapping("web/list")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })

    public Response getLists(FlowShip flowShip) {

        //List<User> pageInfo = userService.getInformation(deptId);

        response = Response.returnResponse(SUCCESS);
        Map<String,Object> map=new HashMap<String,Object>();
        List<Map<String,Object>> list=userService.selectCheckedList(flowShip);
        map.put("tree",userService.getList(flowShip));
        map.put("names",list.size()==0||list.get(0)==null?new ArrayList():list );
        response.setData(map);

        return response;
    }




}
